package cn.sc.allinpay.allfavour.business.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.utils.SpUtils;

/**
 * description：管理员密码
 * author：pany
 * on 2018/7/28 17:36
 */
public class ManagerInputPwdDialog extends Dialog implements View.OnClickListener {

    private AppCompatEditText etPwd;
    private OnDialogListener onDialogListener;
    private String orgMagPwd;//原管理员密码

    public ManagerInputPwdDialog(@NonNull Context context) {
        super(context);
        orgMagPwd = SpUtils.get(getContext(),Constant.SPNAME, Constant.SpKey.MANAGER_PWD_VOID, Constant.MANAGER_PWD_VOID_DEFAULT);
    }

    public ManagerInputPwdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        orgMagPwd = SpUtils.get(getContext(),Constant.SPNAME, Constant.SpKey.MANAGER_PWD_VOID, Constant.MANAGER_PWD_VOID_DEFAULT);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_manager_input_pwd);
        etPwd = findViewById(R.id.etPwd);
        findViewById(R.id.btnConfirm).setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);
    }

    public void setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnConfirm:
                String pwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(getContext(), "管理员密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!pwd.equals(orgMagPwd)) {
                    Toast.makeText(getContext(), "管理员密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onConfirm(pwd);
                }
                break;
            case R.id.btnCancel:
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onCancel();
                }
                break;
        }
    }

    public static abstract class OnDialogListener {

        protected abstract void onConfirm(String input);

        protected void onCancel() {
        }
    }
}
